package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TemperatureAdapter extends BaseAdapter{

	private static final String TAG = "TwAdapter";
	
	//private ImageSwitcher mImageLoader;
	private int mCount;
	private Context mContext;
	private JSONArray twJsonArray =null;
	
	public TemperatureAdapter(int count, Context context, JSONArray twJsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.twJsonArray = twJsonArray;
		//mImageLoader = new ImageSwitcher(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_temperature, null);
			viewholder = new ViewHolder();
			viewholder.tv_sj = (TextView)convertView.findViewById(R.id.tv_sj);
			viewholder.tv_tw = (TextView)convertView.findViewById(R.id.tv_tw);
			viewholder.tv_xy = (TextView)convertView.findViewById(R.id.tv_xy);
			convertView.setTag(viewholder);
			
			try {
				JSONObject xt = twJsonArray.getJSONObject(position);
				if(xt!=null)
				{
					viewholder.tv_sj.setText(xt.getString("CLSJ"));
					if(xt.getString("TW")!=null&&xt.getString("TW")!="")
						viewholder.tv_tw.setText(xt.getString("TW")+"℃");
					viewholder.tv_xy.setText(xt.getString("XY1")+"/"+xt.getString("XY2"));
				}
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(TAG, e.toString());
			}
		
		return convertView;
	}
	
	static class ViewHolder {
		//ImageView imgPatient;
		TextView tv_sj;
		TextView tv_tw;
		TextView tv_xy;
	}
}
